var API      = '1.php',
    domains  = {},
    $eventer = $({});

function init_events() {
    var $domains = $('#domains'),
        $doc = $(document);

    $domains.attr('disabled', false);

    $doc.on('click', '#btn-start', function() {
        var domain = $domains.val();
        $domains.attr('disabled', true);

        domains = {};   //清空之前的数据

        if (domain.length < 4) {    //a.cc这种应该是最短的域名了
            alert('请输入域名');

            return false;
        }

        domain.replace(/\r/g, '');

        var domainRecords = domain.split("\n");

        if (domainRecords.lenth < 1) {
            alert('请输入域名');

            return false;
        }

        var id = 1;

        $.each(domainRecords, function(k, v) {
            v = $.trim(v);

            if (v.length < 4) {
                return false;
            }

            domains[v] = {
                id     : id,
                domain : v,
                status : 'a'
            };

            id++;
        });

        $eventer.trigger('domains:change');

        $('#btn-start').attr('id', 'btn-stop').html('停止');

        check_next();
    })

    $doc.on('click', '#btn-stop', function() {
        $('#btn-stop').attr('id', 'btn-start').html('开始');
        $domains.attr('disabled', false);
        stop();
    })

    $eventer.on('domains:change', function() {
        render_list();
    })

    $eventer.on('check_intercept:finish', function() {
        check_next();
    })
}

function stop() {
    $.each(domains, function(k, v) {
        if (v.status == 'a' || v.status == 'b') {
            v.status = 'c';
        }
    })

    $eventer.trigger('domains:change');
}

function change_domain_status(domain, status) {
    if (typeof(domains[domain]) != 'object') {
        return false;
    }

    domains[domain].status = status;
}

function render_list() {
    $('#result-zone').html(template('list-tpl', { records : domains}));
}

template.helper('get_status_cn', function(status) {
    var cn = {
        a : '<span style="color:#666">正在排队中</span>',
            b : '<span style="margin-right:10px; color:green;">正在查询中</span><img src="res/1.gif">',
            c : '停止'
        },
        r = status;

    if (typeof(cn[status]) != 'undefined') {
        r = cn[status];
    } else if (status != '域名正常') {
        r = '<span style="color:red">' + status + '</span>';
    }

    return r;
})

/**
 * check_next
 * 找出下一条正在排队中需要查询的域名
 *
 * @access public
 * @return void
 */
function check_next() {
    var domain = '';

    $.each(domains, function(k, v) {
        if (v.status == 'a') {
            domain = v.domain;

            return false;
        }
    })

    if (domain.length < 1) {
        $('#btn-stop').attr('id', 'btn-start').html('开始');
        $('#domains').attr('disabled', false);

        alert('查询完毕');

        return false;
    }

    check_intercept(domain);
}

function check_intercept(domain) {
    change_domain_status(domain, 'b');
    $eventer.trigger('domains:change');

    $.getJSON(API, {domain: domain}, function(j){
        var status = typeof(j.m) == 'object' ? j.m.msg : j.m;

        change_domain_status(domain, status);

        $eventer.trigger('domains:change');
        $eventer.trigger('check_intercept:finish');
    })
}

init_events();
